package com.example.yuejianapp14.Activity.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.JavaBean.User;
import com.example.yuejianapp14.Activity.Model.ClassModel;
import com.example.yuejianapp14.Activity.Model.LoginModel;
import com.example.yuejianapp14.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private Button Log;
    private Button Reg;
    private EditText phone, psd;
    SharedPreferences sp;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();

//        Log.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//                startActivity(intent);
//
//
//            }
//        });


        Reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });


    }


    public void initViews() {
        Log = (Button) findViewById(R.id.Login);
        Reg = (Button) findViewById(R.id.Reg);
        phone = (EditText) findViewById(R.id.phone);
        psd = (EditText) findViewById(R.id.psd);
    }


    private void initEvents() {
        Log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone.getText().toString().trim().equals("") || psd.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }

            }
        });
        Reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }


    private void login() {
        LoginModel LoginModel = new LoginModel();
        Call<User> call = LoginModel.login(phone.getText().toString().trim(), psd.getText().toString().trim());

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.body().getUid().equals("0")) {
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    sp = getSharedPreferences("uid", Context.MODE_PRIVATE);
                    editor = sp.edit();
                    editor.putString("uid", response.body().getUid());
                    editor.commit();
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String et_user = data.getStringExtra("user");
        phone.setText(et_user);
    }








}
